var echarts = require("echarts");

var _default = echarts.extendComponentView({
  type: 'bingmap',
  render: function (bingMapModel, ecModel, api) {
    var rendering = true;
    var bingmap = bingMapModel.getBingMap();
    var viewportRoot = api.getZr().painter.getViewportRoot();
    var coordSys = bingMapModel.coordinateSystem;

    var viewChangeHandler = function (arg) {
      console.log('viewChangeHandler', rendering, arg)
      if (rendering) {
        return;
      }

      var offsetEl = viewportRoot.parentNode.parentNode.parentNode;
      var mapOffset = [-parseInt(offsetEl.style.left, 10) || 0, -parseInt(offsetEl.style.top, 10) || 0];
      console.log('mapOffset', mapOffset)
      viewportRoot.style.left = mapOffset[0] + 'px';
      viewportRoot.style.top = mapOffset[1] + 'px';
      coordSys.setMapOffset(mapOffset);
      bingMapModel.__mapOffset = mapOffset;
      api.dispatchAction({
        type: 'bingmapRoam'
      });
    };

    Microsoft.Maps.Events.removeHandler(this._oldViewChangeHandler)
    this._oldViewChangeHandler = Microsoft.Maps.Events.addHandler(bingmap, 'viewchange', viewChangeHandler)

    rendering = false;
  }
});

module.exports = _default;